package cn.vcjf.weapp.common.web;

import cn.vcjf.weapp.api.payment.qfwxpay.model.VcQfPayRes;
import cn.vcjf.weapp.common.exception.ServiceException;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.io.Serializable;
import java.util.List;

/**
 * @Auther: zhengzihang
 * @Date: 2018/7/4 15:20
 * @Description:
 * {
 *   code: 0,
 *   msg: "",
 *   count: 1000,
 *   data: []
 * }
 * id
 * value
 * label
 * type
 * description
 * sort
 * parentId
 * createBy
 * createDate
 * updateBy
 * updateDate
 * remarks
 * delFlag
 */
public class TableResult implements Serializable {
	private static final int SUCCESS=0;
	private static final int ERROR=1;

	Integer code;
	String msg;
	/** 总数
     */
	private int total = 0;

	/**
	 * 总页数
	 */
	private int pages = 0;
	/**
	 * 每页显示条数，默认 10
	 */
	private int size = 10;
	/**
	 * 当前页
	 */
	private int current = 1;

	Object data;

	/**
	 * valid框架校验参数
	 *
	 * @param result
	 */
	public static void checkParams(BindingResult result){
		if(result.hasErrors()){
			List<FieldError> errorList = result.getFieldErrors();
			StringBuilder stringBuilder = new StringBuilder();
			for(FieldError error : errorList){
				System.out.println(error.getField() + "*" + error.getDefaultMessage());
				if (StringUtils.isNotBlank(error.getDefaultMessage())){
					stringBuilder.append(error.getDefaultMessage() +",");
				}else {
					stringBuilder.append(error.getField() + "不能为空,");
				}
			}
			throw new ServiceException(stringBuilder.substring(0,stringBuilder.length()-1));
		}
	}
	public TableResult(){
		this.code=SUCCESS;
		this.msg="ok";
	}
	public static TableResult Result(Boolean b){
		if(b){
			return new TableResult();
		}
		return ErrorResult("error");
	}
	public static TableResult Result(int row){
		if(row>0){
			return new TableResult();
		}
		return ErrorResult("error:row = 0");
	}
	public static TableResult Result(VcQfPayRes res){
		TableResult result = TableResult.Result(res.isOK());
		result.setMsg(res.isOK()?res.getMsg():res.getErrMsg());
		result.data = res;
		return result;
	}

	public static TableResult OKResult(){
		return new TableResult();
	}
	public static TableResult OKResult(Page data){
		return new TableResult(data);
	}
	public static TableResult OKResult(Object data){
		return new TableResult(data);
	}
	public static TableResult OKResult(String msg){
		TableResult result=new TableResult();
		result.setMsg(msg);
		return result;
	}
	public static TableResult DateResult(String data){
		TableResult result=new TableResult();
		result.setMsg("ok");
		result.setData(data);
		return result;
	}
	public static TableResult ErrorResult(String msg){
		return new TableResult(msg);
	}
	public static TableResult ErrorResult(){
		return new TableResult("ERROR");
	}
	public static TableResult ErrorResult(Throwable t){
		return new TableResult(t);
	}

	public static TableResult TokenError(String msg){
		TableResult result = new TableResult();
		result.setCode(-1);
		result.setMsg(msg);
		return result;
	}
	public TableResult(Object data, Integer count){
		this();
		this.data=data;
		this.setTotal(count);
	}
	public TableResult(Object data){
		this();
		this.data=data;
	}
	public TableResult(Page data){
		this();
		if(data != null){
			this.data = data.getRecords();
			this.setTotal(data.getTotal());
			this.setPages(data.getPages());
			this.setSize(data.getSize());
			this.setCurrent(data.getCurrent());
		}
	}
	public TableResult(String msg){
		this.code=ERROR;
		this.msg=msg;
	}
	public TableResult(Throwable t){
		this.code=ERROR;
		this.msg=t.getMessage();
	}

	public Integer getCode() {
		return code;
	}
	public void setCode(Integer code) {
		this.code = code;
	}
	public void setCode(boolean b) {
		if(b){
			this.code=SUCCESS;
		}else {
			this.code=ERROR;
		}
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public boolean OK() {
		return this.code == SUCCESS;
	}
	public boolean ERROR() {
		return this.code == ERROR;
	}
}
